package org.jeecg;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Envelope;
import ctp.thosttraderapi.CThostFtdcTradeField;
import org.jeecg.modules.API.until.RabbitmqUntil;
import org.jeecg.modules.API.until.YGCTPMsg;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class sampletest0 extends YGCTPMsg {

    public sampletest0(Channel channel) throws IOException, TimeoutException {
        super(channel);
    }

    static Channel channel = null;
    public static void main(String[] args) throws IOException, TimeoutException {
        System.loadLibrary("thostmduserapi_se");
        System.loadLibrary("thostmduserapi_wrap");
        System.loadLibrary("thosttraderapi_se");
        System.loadLibrary("thosttraderapi_wrap");
       channel = RabbitmqUntil.getMqChannel();
        sampletest0 st = new sampletest0(channel);
        st.subscribeMsg("zcc", "zxc", "js");
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope,
                               AMQP.BasicProperties properties, byte[] body)
            throws IOException {

        if (envelope.getRoutingKey().equals("js")) {
            System.out.println("成交js。。。。。。。。。。。。。。。。");

            //获得Object类型的成交信息
            String msg = new String(body, "UTF-8");
            JSONObject msg_json = JSONObject.parseObject(msg);
            Object object = msg_json.get("pTrade");

            //Object类型转为CThostFtdcTradeField类型
            ObjectMapper objectMapper = new ObjectMapper();
            CThostFtdcTradeField pTrade =  objectMapper.convertValue(object, CThostFtdcTradeField.class);
            System.out.println(pTrade.getBrokerID());

            channel.basicAck(envelope.getDeliveryTag(), false);
        } else {
            //不符合RoutingKey为OnRtnTrade的消息则拒绝重回mq等待消费
            channel.basicNack(envelope.getDeliveryTag(), true, true);
        }
    }

}
